package com.trafficsys.modules.loginAndRegister.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trafficsys.modules.entity.DriveCard;
import com.trafficsys.modules.entity.Driver;
import com.trafficsys.modules.entity.Ticket;
import com.trafficsys.modules.entity.TravelCard;
import com.trafficsys.modules.loginAndRegister.dao.MyNodeDao;
import com.trafficsys.modules.loginAndRegister.pojo.TicketAndRule;
import com.trafficsys.modules.loginAndRegister.service.DriverService;
import com.trafficsys.modules.loginAndRegister.util.Limit;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.HashMap;

/**
 * @Author Zhao Bin
 * @Date 2021/9/28 13:18
 * @Version 1.0
 */
@Controller
@RequestMapping("/api")
public class DriverController {
    @Autowired
    private DriverService driverService;
    @Autowired
    private Limit limit;
    /*********************************************   以下驾驶员功能  *********************************************************
     * 驾驶员登录（身份证、手机号码结合密码登录）    驾驶员功能
     *  posy:
     *  http://localhost:8080/api/driverLogin?&phone=15730593398&password=15730593398
     *  json:
     *  http://localhost:8080/api/driverLogin
     *  {
     *     "cardId":"500235199805059270",
     *     "password":"15730593398"
     *  }
     *  内网穿透地址：动态变化的
     *  http://yvk9de.natappfree.cc/api/driverLogin
     */
    @PostMapping(value = "/driverLogin", consumes = "application/json")
    @ResponseBody//将数据返回到body展示
    public HashMap<String,Object> driverLogin(@RequestBody Driver driver, HttpServletRequest request){
        HashMap<String,Object> map=new HashMap<>();
        //限制访问频率
        if(limit.limit(request).get("state").equals("Pass")){
             map=driverService.driverLogin(driver);
            return map;
        }else{
            map.put("info","您的访问频率过高,请在一分钟后访问");
        }
        return map;
    }
    /**
     *  驾驶员注册   驾驶员功能
     *  post:
     *  http://localhost:8080/api/register?cardId=500235199805059270&name=赵彬&phone=15730593398&password=15730593398
     *  json:
     *  http://localhost:8080/api/register
     *  {
     *     "cardId":"500235199850509270",
     *     "name":"赵彬",
     *     "password":"15730593398",
     *     "phone":"15730593398"
     * }
     */
    @PostMapping(value = "/register", consumes = "application/json")
    @ResponseBody
    public HashMap<String,Object> register(@RequestBody Driver driver){
        return driverService.register(driver);
    }
    /**
     *  驾驶员查看驾驶证信息  驾驶员功能
     *  127.0.0.1:8080/api/getDriveCardByCardId?current=1&size=2&cardId=500235199805059270
     */
    @GetMapping("/getDriveCardByCardId")
    @ResponseBody
    public IPage<DriveCard> getDriveCardByCardId(@RequestParam("current") int current, @RequestParam("size") int size, @RequestParam("cardId")String cardId){
        Page<DriveCard> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return driverService.getDriveCardByCardId(page, cardId);
    }
    /**
     *  驾驶员查看行驶证信息  驾驶员功能
     *  127.0.0.1:8080/api/getTravelCardByCardId?current=1&size=3&cardId=500235199805059270
     */
    @GetMapping("/getTravelCardByCardId")
    @ResponseBody
    public IPage<TravelCard> getTravelCardByCardId(@RequestParam("current") int current, @RequestParam("size") int size, @RequestParam("cardId")String cardId){
        Page<TravelCard> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return driverService.getTravelCardByCardId(page, cardId);
    }
    /**
     *  根据phone查询到驾驶员信息
     *  http://localhost:8080/api/getDriverByPhone/15730593398
     */
    @GetMapping("/getDriverByPhone/{phone}")
    @ResponseBody
    public HashMap<String,Object> getDriverByPhone(@PathVariable String phone){
        return driverService.getDriverByPhone(phone);
    }
    /**
     *  修改驾驶员信息
     *  http://localhost:8080/api/updateDriver
     {

     }
     */
    @PostMapping(value = "/updateDriver",consumes = "application/json")
    @ResponseBody
    public HashMap<String,Object> updateDriver(@RequestBody Driver driver){return driverService.updateDriver(driver);}
    /**
     * 查询权限
     */
    @GetMapping("/driverSelectPower")
    @ResponseBody
    public HashMap<String,Object> driverSelectPower(String cardId){
        return driverService.driverSelectPower(cardId);
    }
    /**
     * 主页面加载时 根据cardId查询该用户所有信息
     */
    @GetMapping("/getDriverByCardId")
    @ResponseBody
    public HashMap<String,Object> getDriverByCardId(String cardId){
        return driverService.getDriverByCardId(cardId);
    }
    /**
     * 退出系统
     */
    @PostMapping(value = "/driverLogout", consumes = "application/json")
    @ResponseBody//将数据返回到body展示
    public HashMap<String,Object> driverLogout(@RequestBody Driver driver){
        return driverService.driverLogout(driver);
    }
    /**
     * 根据拥有的车辆车牌号查询违章信息  已处理罚单信息
     * 127.0.0.1:8080/api/getTicketsByCardId?current=1&size=3&cardId=500235199805059270
     */
    @GetMapping("/getSolvedTicketAndRuleByCardId")
    @ResponseBody
    public IPage<TicketAndRule> getSolvedTicketAndRuleByCardId(@RequestParam("current") int current, @RequestParam("size") int size, @RequestParam("cardId")String cardId){
        Page<TicketAndRule> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return driverService.getSolvedTicketAndRuleByCardId(page, cardId);
    }

    /**
     * 根据拥有的车辆车牌号查询违章信息  已处理罚单信息
     * 127.0.0.1:8080/api/getTicketsByCardId?current=1&size=3&cardId=500235199805059270
     */
    @GetMapping("/getNoSolveTicketAndRuleByCardId")
    @ResponseBody
    public IPage<TicketAndRule> getNoSolveTicketAndRuleByCardId(@RequestParam("current") int current, @RequestParam("size") int size, @RequestParam("cardId")String cardId){
        Page<TicketAndRule> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return driverService.getNoSolveTicketAndRuleByCardId(page, cardId);
    }
    /**
     * 处理罚单 参数 罚单编号ticket id 和 驾驶员身份证号cardId
     */
    @GetMapping("/solveTicket")
    @ResponseBody
    public HashMap<String,Object> solveTicket(@RequestParam("id")String id,@RequestParam("cardId") String cardId,@RequestParam("cutScore")String cutScore){
        return driverService.solveTicket(id,cardId,cutScore);
    }
    /**
     *分页查询所有驾驶证
     */
    @GetMapping("/getAllDriveCard")
    @ResponseBody
    public IPage<DriveCard> getAllDriveCard(@RequestParam("current") int current, @RequestParam("size") int size){
        Page<DriveCard> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return driverService.getAllDriveCard(page);
    }
    /**
     *分页查询所有驾驶证
     */
    @GetMapping("/getAllTravelCard")
    @ResponseBody
    public IPage<TravelCard> getAllTravelCard(@RequestParam("current") int current, @RequestParam("size") int size){
        Page<TravelCard> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return driverService.getAllTravelCard(page);
    }
}
